package com.fm.financemanage.service;

import com.fm.financemanage.exception.BusinessException;
import com.fm.financemanage.mapper.CustomerCompanyAccountConfigMapper;
import com.fm.financemanage.mapper.CustomerCompanyInfoMapper;
import com.fm.financemanage.model.po.CustomerCompanyAccountConfig;
import com.fm.financemanage.model.po.CustomerCompanyInfo;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.company.CompanyAccountConfigOperatorVo;
import com.fm.financemanage.model.vo.company.CustomerCompanyInfoListVo;
import com.fm.financemanage.model.vo.company.CustomerCompanyOperatorVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class CustomerCompanyInfoService {

    @Resource
    private CustomerCompanyInfoMapper customerCompanyInfoMapper;

    @Resource
    private CustomerCompanyAccountConfigMapper customerCompanyAccountConfigMapper;

    public PageVo<List<CustomerCompanyInfoListVo>> getList(String uuid, Integer customerClassifyId, Integer companyInfoClassifyId, Integer accountantId, String name, Integer page, Integer limit) {

        return new PageVo<List<CustomerCompanyInfoListVo>>(customerCompanyInfoMapper.getList(null,uuid, customerClassifyId, companyInfoClassifyId, accountantId, name, (page - 1) * limit, limit),
                customerCompanyInfoMapper.count(null,uuid, customerClassifyId, companyInfoClassifyId, accountantId, name));

    }


    public void operator(CustomerCompanyOperatorVo companyOperatorVo) {
        final CustomerCompanyInfo customerCompanyInfo = new CustomerCompanyInfo();
        BeanUtils.copyProperties(companyOperatorVo, customerCompanyInfo);
        if (customerCompanyInfo.getId() == null) {
            customerCompanyInfo.setUuid(UUID.randomUUID().toString().replace("-", ""));
            customerCompanyInfoMapper.insertSelective(customerCompanyInfo);
        } else {
            customerCompanyInfoMapper.updateByPrimaryKeySelective(customerCompanyInfo);
        }
    }


    public void removeById(String uuid) {
        customerCompanyInfoMapper.deleteByUuid(uuid);

    }

    public CustomerCompanyAccountConfig operatorAccount(CompanyAccountConfigOperatorVo customerCompanyAccountConfigVo) {
        final CustomerCompanyAccountConfig customerCompanyAccountConfig = new CustomerCompanyAccountConfig();
        BeanUtils.copyProperties(customerCompanyAccountConfigVo, customerCompanyAccountConfig);
        if (customerCompanyAccountConfig.getId() == null) {
            customerCompanyAccountConfig.setCreateTime(LocalDateTime.now());
            customerCompanyAccountConfig.setUpdateTime(LocalDateTime.now());
            customerCompanyAccountConfig.setUuid(UUID.randomUUID().toString().replace("-", ""));
            Integer count = customerCompanyAccountConfigMapper.countByCompanyIdAndType(customerCompanyAccountConfig.getCustomerId(), customerCompanyAccountConfig.getType());
            if (count > 0) {
                throw new BusinessException("已存在相同平台,无法重复添加账号密码");
            }
            customerCompanyAccountConfigMapper.insertSelective(customerCompanyAccountConfig);
        } else {
            customerCompanyAccountConfigMapper.updateByPrimaryKeySelective(customerCompanyAccountConfig);
        }
        return customerCompanyAccountConfig;

    }

    public void removeByAccountId(String uuid) {
        customerCompanyAccountConfigMapper.deleteByUuid(uuid);

    }
}
